import { defineStore } from "pinia"
import { getUserProfile, getSecuritySettings, getPaymentMethods, updateUserProfile, uploadUserAvatar, updateSecuritySetting, addPaymentMethod } from "@/mocks"
import { ref } from "vue"

const useProfileStore = defineStore("profile", () => {
    // 个人信息状态
    const userInfo = ref({})
    const securityInfo = ref({})
    const payments = ref([])
    const isLoading = ref(false)

    // 获取个人信息
    const fetchUserProfile = async () => {
        isLoading.value = true
        try {
            const res = await getUserProfile()
            userInfo.value = res.data
            return res.data
        } catch (error) {
            console.error("获取个人信息失败:", error)
            return null
        } finally {
            isLoading.value = false
        }
    }

    // 获取安全设置
    const fetchSecuritySettings = async () => {
        isLoading.value = true
        try {
            const res = await getSecuritySettings()
            securityInfo.value = res.data
            return res.data
        } catch (error) {
            console.error("获取安全设置失败:", error)
            return null
        } finally {
            isLoading.value = false
        }
    }

    // 获取支付方式
    const fetchPaymentMethods = async () => {
        isLoading.value = true
        try {
            const res = await getPaymentMethods()
            payments.value = res.data
            return res.data
        } catch (error) {
            console.error("获取支付方式失败:", error)
            return []
        } finally {
            isLoading.value = false
        }
    }

    // 更新个人信息
    const updateProfile = async (profileData) => {
        isLoading.value = true
        try {
            const res = await updateUserProfile(profileData)
            userInfo.value = res.data
            return {
                success: true,
                message: res.message
            }
        } catch (error) {
            console.error("更新个人信息失败:", error)
            return {
                success: false,
                message: "更新失败"
            }
        } finally {
            isLoading.value = false
        }
    }

    // 上传头像
    const uploadAvatar = async (fileData) => {
        isLoading.value = true
        try {
            const res = await uploadUserAvatar(fileData)
            userInfo.value.avatar = res.data.avatarUrl
            return {
                success: true,
                avatarUrl: res.data.avatarUrl,
                message: res.message
            }
        } catch (error) {
            console.error("上传头像失败:", error)
            return {
                success: false,
                message: "上传失败"
            }
        } finally {
            isLoading.value = false
        }
    }

    // 更新安全设置
    const updateSecurity = async (settingData) => {
        isLoading.value = true
        try {
            const res = await updateSecuritySetting(settingData)
            securityInfo.value = res.data
            return {
                success: true,
                message: res.message
            }
        } catch (error) {
            console.error("更新安全设置失败:", error)
            return {
                success: false,
                message: "更新失败"
            }
        } finally {
            isLoading.value = false
        }
    }

    // 添加支付方式
    const addPayment = async (paymentData) => {
        isLoading.value = true
        try {
            const res = await addPaymentMethod(paymentData)
            payments.value.push(res.data)
            return {
                success: true,
                message: res.message
            }
        } catch (error) {
            console.error("添加支付方式失败:", error)
            return {
                success: false,
                message: "添加失败"
            }
        } finally {
            isLoading.value = false
        }
    }

    return {
        userInfo,
        securityInfo,
        payments,
        isLoading,
        fetchUserProfile,
        fetchSecuritySettings,
        fetchPaymentMethods,
        updateProfile,
        uploadAvatar,
        updateSecurity,
        addPayment
    }
}, {
    persist: true
})

export default useProfileStore 